﻿<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>应用管理</title>
  <meta name="renderer" content="webkit">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
  <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
  <link rel="stylesheet" href="/plugins/layuiadmin/style/admin.css" media="all">
  <link rel="stylesheet" href="/plugins/layuiadmin/style/template.css" media="all">
</head>
<body>
  <div class="layui-fluid">
    <div class="layadmin-caller">
      <div class="LAY-app-message-btns" style="margin-bottom: 10px;">
            <button class="layui-btn layui-btn-normal layui-btn-list" data-type="install"><i class="layui-icon layui-icon-component"></i>安装应用</button>
            <button class="layui-btn layui-btn-warm layui-btn-list" data-type="new"><i class="layui-icon layui-icon-add-circle"></i>新建应用</button>
            <button class="layui-btn layui-btn-list" data-type="appstore"><i class="layui-icon layui-icon-app"></i>应用商店</button>
            <div class="layui-form" lay-filter="dds-search-form" id="search-form" style="float:right">
                <div class="layui-inline">
                    <input class="layui-input" name="keyword" id="keyword" placeholder="请输入搜索条件">
                    <input type="hidden" name="apptype" id="apptype" value="0">
                </div>
                <button class="layui-btn" lay-submit lay-filter="btn_search"><i class="layui-icon layui-icon-search"></i>搜索</button>
            </div>
      </div>
      <div class="layui-tab layui-tab-brief caller-tab" lay-filter="apptype">
        <ul class="layui-tab-title">
          <li lay-id="" class="layui-this">全部应用</li>
          <li lay-id="system">系统应用<span class="layui-badge-dot"></span></li>
          <li lay-id="plugin">应用插件</li>
          <li lay-id="user">用户应用</li>
        </ul>
      </div>
      <div class="caller-contar" id="applist">
      </div>
      <div id="pager"></div>
    </div>
   <script id="approw" type="text/html">
    {{#  layui.each(d, function(index, item){ }}
    <div class="caller-item">
        <a class="appinfo" data-type="appinfo" data="{{item.id}}" href="javascript:void();">
        {{#  if(item.icon !=""){ }}
        <img src="{{item.apppath}}{{item.icon}}" class="caller-img caller-fl layadmin-homepage-pad-img" style="margin-left: 5px;">
        {{#} else{}}
        <img src="statics/images/app.jpg" alt="" class="caller-img caller-fl layadmin-homepage-pad-img" style="margin-left: 5px;">
        {{#  } }} 
        </a>
        <div class="caller-main caller-fl">       
        <p><strong><a class="appinfo" data-type="appinfo" data="{{item.id}}" href="javascript:void();">{{ item.name}}</a></strong><em>V{{ item.version}}</em>
            {{#  if(item.apptype === 'system'){ }}
            &nbsp;&nbsp;<span class="layui-badge-dot" title="系统应用"></span>
            {{#  } }} 
         </p>
        <p class="caller-adds">作者：{{ item.author}}</p>
        <div class="caller-iconset">
        {{ item.intro}}
        </div>
        </div>
        <div class="layui-btn-group" style="float:right;">
        <button class="layui-btn layui-btn-warm layui-btn-sm open" data="{{item.apppath}}" data-target="{{item.target}}" data-name="{{ item.name}}" data-adminindex="{{item.adminindex}}" data-type="open"><i class="layui-icon layui-icon-website"></i>打开</button>
        <button class="layui-btn layui-btn-sm" data="{{item.id}}" data-type="set"><i class="layui-icon layui-icon-set"></i>设置</button>
        <button class="layui-btn layui-btn-normal layui-btn-sm" data="{{item.id}}" data-type="upgrade"><i class="layui-icon layui-icon-upload-circle"></i>升级</button>
        <button class="layui-btn layui-btn-danger layui-btn-sm" data="{{item.id}}" data-type="delete"><i class="layui-icon"></i>卸载</button>
        </div>
    </div>
    {{#  }); }}
    </script>
  <script src="/plugins/layui/layui.js"></script>
  <script type="text/javascript" src="/plugins/common/login.js"></script>
  <script>
  layui.config({
    base: '/plugins/layuiadmin/'
  }).extend({
    index: 'lib/index'
  }).use(['index', 'laypage','layer','laytpl','form','element'], function(){
      var $ = layui.$, laypage = layui.laypage, layer = layui.layer, laytpl = layui.laytpl, form = layui.form, element = layui.element;

      function GetFirstList(where) {
          layer.load();
          $.ajax({
            type: "get",
            url: "app_manage",
            data: where,
            dataType: "json",
            async:false,
            success: function (res) {
                if (res.code == 0) {
                    laytpl(approw.innerHTML).render(res.data, function(html){
                      applist.innerHTML = html;
                    });
                    laypage.render({
                        elem: 'pager',
                        count: res.count,
                        limit: res.pagesize,
                        jump: function (obj, first) {
                            if (!first) {
                                var where = {keyword: $('#keyword').val(), pageindex: obj.curr};
                                GetList(where);
                            }
                        }
                    });
                    layer.closeAll('loading'); 
                }
                else if (res.code == 110)
                {
                    layer.closeAll('loading'); 
                    layer.alert(res.msg, function () {
                        ReLogin();
                    });
                }
                else {
                    layer.closeAll('loading'); 
                    layer.alert(res.msg);
                }
            }
          });
      }

      function GetList(where) {
          layer.load();
          $.ajax({
            type: "get",
            url: "app_manage",
            data: where,
            dataType: "json",
            async:false,
            success: function (res) {
                if (res.code == 0) {
                    laytpl(approw.innerHTML).render(res.data, function(html){
                      applist.innerHTML = html;
                    });
                    layer.closeAll('loading'); 
                }
                else if (res.code == 110)
                {
                    layer.closeAll('loading'); 
                    layer.alert(res.msg, function () {
                        ReLogin();
                    });
                }
                else {
                    layer.closeAll('loading'); 
                    layer.alert(res.msg);
                }
            }
          });
      }

      window.GetFirstList= function(){
       GetFirstList();
      };

      GetFirstList();

      form.on('submit(btn_search)', function(data){
          var field = data.field;
          GetFirstList(field);
      });

      element.on('tab(apptype)', function () {
          var apptype = this.getAttribute('lay-id');
          $('#apptype').val(apptype);
          GetFirstList({ apptype: apptype,keyword:$('#keyword').val() });
      });

      var active = {
          open: function () {
              var apppath = $(this).attr('data');
              var adminindex = $(this).attr('data-adminindex');
              var target = $(this).attr('data-target');
              var appname = $(this).attr('data-name');

              if (adminindex.indexOf("http://")==-1&&adminindex.indexOf("https://")==-1) {
                  adminindex = apppath + adminindex;
              }

              if (target == "_parent") {
                  top.layui.index.openTabsPage(adminindex, appname);
              }
              else if (target=="_open")
              {
                  layer.open({
                    type: 2
                    ,title: appname
                    ,content: adminindex
                    ,maxmin: true
                    ,area: ['700px', '600px']
                 });
              }
              else if (target=="_open1")
              {
                  layer.open({
                    type: 2
                    ,title: appname
                    ,content: adminindex
                    ,maxmin: true
                    ,area: ['700px', '600px']
                    ,btn: ['确定', '取消']
                    ,yes: function(index, layero){
                       var submit = layero.find('iframe').contents().find("#btn_submit");
                       submit.trigger('click');
                    }
                  });
              }
              else {
                  window.open(adminindex);
              }
          },
          set: function () {
              var id = $(this).attr('data');
              layer.open({
                type: 2
                ,title: '应用设置'
                ,content: 'app_set.html#/id='+id
                ,maxmin: true
                ,area: ['700px', '600px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                   var submit = layero.find('iframe').contents().find("#btn_submit");
                   submit.trigger('click');
                }
               });
          },
          upgrade: function () {
             var id = $(this).attr('data');
             layer.open({
                type: 2
                ,title: '应用升级'
                ,content: 'app_upgrade.html#/id='+id
                ,maxmin: true
                ,area: ['500px', '250px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                var submit = layero.find('iframe').contents().find("#btn_submit");
                submit.trigger('click');
                }
             });
          },
          delete: function () {
             var id = $(this).attr('data');
              layer.confirm('您确定要卸载该应用吗？卸载可能引起相关联的应用不能使用。', function (index) {
              layer.load();
              $.ajax({
                    type: "post",
                    url: "app_manage",
                    data: {id:id},
                    dataType: "json",
                    success: function (data) {
                        layer.closeAll('loading'); 
                        if (data.code == 0) {
                            GetFirstList();
                            layer.msg(data.msg);
                        }
                        else {
                            layer.alert(data.msg);
                        }
                    }
              });
            });
          },
          install: function () {
             layer.open({
                type: 2
                ,title: '安装应用'
                ,content: 'app_install.html'
                ,maxmin: true
                ,area: ['650px', '500px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                var submit = layero.find('iframe').contents().find("#btn_submit");
                submit.trigger('click');
                }
             });
          },
          new: function () {
             layer.open({
                type: 2
                ,title: '新建应用'
                ,content: 'app_add.html'
                ,maxmin: true
                ,area: ['650px', '620px']
                ,btn: ['确定', '取消']
                ,yes: function(index, layero){
                var submit = layero.find('iframe').contents().find("#btn_submit");
                submit.trigger('click');
                }
             });
          },
          appstore: function () {
             window.open('http://appstore.fangpage.com');
          },
          appinfo: function () {
             var id = $(this).attr('data');
             layer.open({
                type: 2
                ,title: '应用信息'
                ,content: 'app_info.html#/id='+id
                ,maxmin: true
                ,area: ['650px', '550px']
                ,btn: ['关闭']
             });
          }
      }; 

      $('#applist').on('click', '.layui-btn', function () {
          var type = $(this).data('type');
          active[type] ? active[type].call(this) : '';
      });

      $('.LAY-app-message-btns').on('click', '.layui-btn.layui-btn-list', function () {
          var type = $(this).data('type');
          active[type] ? active[type].call(this) : '';
      });

      $('#applist').on('click', '.appinfo', function () {
          var type = $(this).data('type');
          active[type] ? active[type].call(this) : '';
      });

  });
  </script>
</body>
</html>